package com.hospital.doctor.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.doctor.pojo.Patient;
import com.hospital.doctor.service.MedicalService;
import com.hospital.doctor.service.PatientService;
import com.hospital.doctor.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@CrossOrigin
public class PatientController {

    @Autowired
    private PatientService patientService;

    @Autowired
    private MedicalService medicalService;

    @Autowired
    private HttpServletRequest request;


    //查询所用并分页
    @RequestMapping("/selectAll/{page}/{size}")
    public Map<String,Object> selectAll(@PathVariable("page") int page,@PathVariable("size") int size,HttpServletRequest request){
        String token = request.getHeader("token");
        Claim kId = JWTUtils.verify(token).getClaim("kid");

        Patient patient = new Patient();
        patient.setkId(Integer.valueOf(kId.asString()));
        PageHelper.startPage(page,size);
        PageInfo<Patient> patientPageInfo = new PageInfo<>(patientService.selectAll(patient));
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",patientPageInfo);

        System.out.println(request.getHeader("token"));
        return map;
    }
    //查询所有不分页
    @RequestMapping("/selectPatient")
    public List<Patient> selectPatient(HttpServletRequest request){
        String token = request.getHeader("token");
        Claim kId = JWTUtils.verify(token).getClaim("kid");
        Patient patient = new Patient();
        patient.setkId(Integer.valueOf(kId.asString()));
        return patientService.selectAll(patient);
    }

    //搜索框模糊查询，并分页
    @RequestMapping("/selectBypName/{pName}/{page}/{size}")
    public Map<String,Object> selectBypName(@PathVariable("pName") String pName, @PathVariable("page")int page, @PathVariable("size") int size){

        String token = request.getHeader("token");
        Claim kId = JWTUtils.verify(token).getClaim("kid");
        Patient patient = new Patient();
        patient.setkId(Integer.valueOf(kId.asString()));
        PageHelper.startPage(page,size);
        List<Patient> patients = patientService.selectByName(pName);
        PageInfo<Patient> patientPageInfo = new PageInfo<>(patientService.selectByName(pName));
        Map<String,Object> map = new HashMap<>();
//        List<Patient> patients = patientService.selectByName(pName);
        map.put("msg",patientPageInfo);
        return map;
    }
    //搜索框模糊查询，不分页
    @RequestMapping("/selectBypNameNo/{pName}")
    public Map<String,Object> selectBypNameNo(@PathVariable("pName") String pName){
        Map<String,Object> map = new HashMap<>();
        List<Patient> patients = patientService.selectByName(pName);
        map.put("msg",patients);
        return map;
    }


    //查看更多
    @RequestMapping("/selectById/{id}")
    public Map<String,Object> selectById(@PathVariable("id") int id){
        Patient patient = patientService.selectById(id);
        System.out.println(patient.toString());
//        List<Medical> medical = medicalService.selectById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("msg",patient);
//        map.put("msgs",medical);
        return map;
    }
    //修改
    @RequestMapping("/updateById")
    public Map<String,Object> updateById(@RequestBody Patient patient){
        Map<String, Object> map = new HashMap<>();
        int i = patientService.updateById(patient);
        map.put("msg",i);
        return map;
    }
    //出院
    @RequestMapping("/updateOutTime/{pId}")
    public Map<String,Object> updateOutTime(@PathVariable int pId){
        Map<String, Object> map = new HashMap<>();
        int i = patientService.updateOutTime(pId);
        map.put("msg",i);
        return map;
    }



}
